﻿using HY.Common;
using HY.Entity;
using HY.Service;
using HY.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HY.Web.Controllers
{
    public class HomeController : Controller
    {
        HomeService homeService = new HomeService();

        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public JsonResult Login(string userName, string password, string validateCode)
        {
            ReturnModel returnModel = new ReturnModel();
            if (CheckValidateCode(validateCode))
            {
                V_UserInfo userinfo = homeService.UserLogin(userName, password);
                if (userinfo != null)
                {
                    GlobalSupport.CurrentUser = userinfo;
                    LogHelper.Info(new LogInfo() { UserID = userinfo.UserID, LogMessage = "登录成功！", LogType = LogType.Login });
                }
                else
                {
                    returnModel.code = 1;
                    returnModel.msg = "用户名或密码错误！";
                }
            }
            else
            {
                returnModel.code = -1;
                returnModel.msg = "验证码错误！";
            }
            return Json(returnModel);
        }

        [AllowAnonymous]
        public ActionResult LoginOut()
        {
            ReturnModel returnModel = new ReturnModel();
            WebHelper.RemoveCookie(GlobalKey.UserInfo);
            return RedirectToAction("Desktop");
        }

        [AllowAnonymous]
        public ActionResult Index()
        {
            V_UserInfo userInfo = GlobalSupport.CurrentUser;

            //演示，屏蔽登录
            if (userInfo == null)
            {
                userInfo = homeService.UserLogin("admin", "123456");
                GlobalSupport.CurrentUser = userInfo;
            }

            if (userInfo != null)
            {
                //var menuList = GlobalSupport.MyMenus.Where(w => new int[] { 1, 7, 11, 14, 18, 26, 1033, 1041 }.Contains(w.ID) || new int[] { 1, 7, 11, 14, 18, 26, 1033, 1041 }.Contains(w.ParentID)).OrderBy(o => o.ModuleSort).ToList();
                var menuList = GlobalSupport.MyMenus.OrderBy(o => o.ModuleSort).ToList();
                return View(menuList);
            }
            else
                return View("Login");
        }

        [AllowAnonymous]
        public ActionResult Analysis()
        {
            V_UserInfo userInfo = GlobalSupport.CurrentUser;

            //演示，屏蔽登录
            if (userInfo == null)
            {
                userInfo = homeService.UserLogin("admin", "123456");
                GlobalSupport.CurrentUser = userInfo;
            }

            if (userInfo != null)
            {
                var menuList = GlobalSupport.MyMenus.Where(w => w.ParentID == 21).OrderBy(o => o.ModuleSort).ToList();
                return View(menuList);
            }
            else
                return View("Login");
        }

        [AllowAnonymous]
        public ActionResult Desktop()
        {
            return View();
        }

        public ActionResult Show3D()
        {
            return View();
        }

        #region 自定义错误页面
        [AllowAnonymous]
        public ActionResult Error_404()
        {
            return View();
        }
        #endregion

        #region 验证码
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult ValidateCode()
        {
            int width = 90;
            int height = 26;
            int fontsize = 18;
            string code = string.Empty;
            byte[] bytes = ValidateCodeHelper.GetValidateCode(out code, 4, width, height, fontsize);
            WebHelper.SetSession(GlobalKey.ValidateCode, code);
            return File(bytes, @"image/jpeg");
        }
        /// <summary>
        /// 校验验证码
        /// </summary>
        /// <param name="validateCode"></param>
        /// <returns></returns>
        private bool CheckValidateCode(string validateCode)
        {
            string showValidateCode = WebHelper.GetSession(GlobalKey.ValidateCode) as string;
            if (!string.IsNullOrEmpty(showValidateCode) && validateCode.ToLower() == showValidateCode.ToLower())
                return true;
            else
                return false;
        }
        #endregion
    }
}